<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="日期" prop="payDate">
        <el-date-picker clearable size="small" v-model="queryParams.payDate" type="date" value-format="yyyy-MM-dd"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="姓名" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入姓名" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="payrecordList" :span-method="objectSpanMethod">

      <el-table-column label="日期" align="center" prop="payDate" />
      <el-table-column label="姓名" align="center" prop="userName" />
      <el-table-column label="结算金额" align="center" prop="payMoney" />
      <el-table-column label="利润" align="center" prop="profit" v-if="checkPermi(['system:stock:seeCost'])" />
      <el-table-column label="是否有效" align="center" prop="effect">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_effect" :value="scope.row.effect" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="主表id"  align="center" prop="payRecordId" /> -->
      <el-table-column label="品名" align="center" prop="name" />
      <el-table-column label="款号" align="center" prop="itemNum" />
      <el-table-column label="尺码" align="center" prop="sizeNum" />
      <el-table-column label="单价" align="center" prop="price" />
      <el-table-column label="成本" align="center" prop="cost" v-if="checkPermi(['system:stock:seeCost'])" />
      <el-table-column label="折扣" align="center" prop="currentDiscount" width="50">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_discount_value" :value="scope.row.currentDiscount" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="购买数量" align="center" prop="subCount" /> -->
      <el-table-column :label=sumLable align="center" prop="subCount" width="120" />

    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

  </div>
</template>

<script>
import { listCount, listPayrecord, getPayrecord, delPayrecord, addPayrecord, updatePayrecord, exportPayrecord } from "@/api/system/payrecord";
import { checkPermi, checkRole } from "@/utils/permission";
export default {
  name: "Payrecord",
  dicts: ['sys_discount_value', 'sys_effect'],
  data() {
    return {
      //库存总数量lable
      stockSum: null,
      //定义合并标识
      payMasterIdArray: [],
      payMasterIdPos: 0,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,

      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 结算记录表格数据
      payrecordList: [],

      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: 'payRecordId',
        isAsc: 'desc',
        payDate: null,
        userId: null,
        userName: null,
        payMoney: null,
        profit: null,
        effect: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getCount();


  },
  methods: {
    checkPermi,
    checkRole,
    hebing() {
      //定义合并标识
      this.payMasterIdArray = [];
      this.payMasterIdPos = 0;
      for (let i = 0; i < this.payrecordList.length; i++) {
        if (i === 0) {
          this.payMasterIdArray.push(1);
          this.payMasterIdPos = 0;
        }
        else {
          if (this.payrecordList[i].payRecordId === this.payrecordList[i - 1].payRecordId) {
            this.payMasterIdArray[this.payMasterIdPos] += 1;
            this.payMasterIdArray.push(0);
          } else {
            this.payMasterIdArray.push(1);
            this.payMasterIdPos = i;
          }
        }

      }


    },
    // 合并的地方也只有两处
    objectSpanMethod({ rowIndex, columnIndex }) {
      console.log(rowIndex + "--" + columnIndex);
      if (columnIndex <= 4) {
        const row1 = this.payMasterIdArray[rowIndex]
        const col1 = row1 > 0 ? 1 : 0
        return {
          rowspan: row1,
          colspan: col1
        }
      }

    },
    getCount() {
      this.loading = true;
      listCount(this.queryParams).then(response => {
        console.log(response.data);
        this.stockSum = response.data;

        this.loading = false;
      });
    },

    /** 查询结算记录列表 */
    getList() {
      this.loading = true;
      listPayrecord(this.queryParams).then(response => {
        this.payrecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        payDate: null,
        userId: null,
        userName: null,
        payMoney: null,
        profit: null,
        effect: null
      };
      this.sysPayRecordSlaveList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.getCount();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },



  },
  computed: {
    sumLable() {
      return '销件(总量:' + this.stockSum + ')'
    }

  },
  watch: {
    payrecordList() {
      this.hebing();
    }
  }
};
</script>
